package top.jlzhou.tools.http;

import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

/**
 * <pre>http请求的HttpClient实现</pre>
 * @ClassName	top.jlzhou.tools.http.ResponseByHttpClientImpl
 * @author	J.L.Zhou
 * @date	2019年3月9日 下午6:59:03
 * @version 	V1.0
 * @Copyright	2019 jlzhou.top Inc. All rights reserved. 
 * 	注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目
 */
public class ResponseByHttpClientImpl implements Response {

	
	private HttpResponse response;
	

	public ResponseByHttpClientImpl(HttpResponse response) {
		super();
		this.response = response;
	}

	@Override
	public int getStatus() {
		return response.getStatusLine().getStatusCode();
	}

	@Override
	public String getBodyString()throws Exception {
		return EntityUtils.toString(response.getEntity(), "UTF-8");
	}
	
	@Override
	public String getBodyString(String charset)throws Exception {
		return EntityUtils.toString(response.getEntity(), charset);
	}

	@Override
	public InputStream getBodyStream()throws Exception {
		return response.getEntity().getContent();
	}
	
	@Override
	public long getContentLength() {
		return response.getEntity().getContentLength();
	}
	
	@Override
	public String getContentEncoding() {
		if(response.getEntity().getContentEncoding()!=null) {
			return response.getEntity().getContentEncoding().getValue();
		}
		return null;
	}
	
	@Override
	public String getContentType() {
		if(response.getEntity().getContentType()!=null) {
			return response.getEntity().getContentType().getValue();
		}
		return null;
	}
	

	@Override
	public Header[] getHeaders(String name) {
		org.apache.http.Header[] hs = response.getHeaders(name);
		if(hs!=null&&hs.length>0) {
			Header[] hh = new Header[hs.length];
			for(int i=0;i<hh.length;i++) {
				hh[i]=new Header(hs[i].getName(), hs[i].getValue());
			}
		}
		return null;
	}

	@Override
	public Header getHeader(String name) {
		if(response.getFirstHeader(name)!=null) {
			return new Header(response.getFirstHeader(name).getName(),response.getFirstHeader(name).getValue());
		}
		return null;
	}
	
	

}
